<?php
/*
 * 后台菜单配置和管理
 * author wangjian 
 *time
 */
if (! defined('BASEPATH')) {
    exit('Access Denied');
}

class Index extends MY_Controller {
	private $username ;//当前登录的用户
	private $ip ; //登录的ip地址
	private $table_ ; //表的前缀
	private $nav_cache_path ;
	function Index(){
		parent::__construct();
		$this->load->library('admin_common');//加载admin的公用方法
		$this->load->model('admin/M_common');
		$this->username = login_name() ; //得到登录的用户名
		$this->ip = get_client_ip() ;
		$this->table_ = $this->admin_common->table_pre('real_data');
		$this->nav_cache_path = __ROOT_PATH__."/".APPPATH."/cache";
	}
	
	function show_nav(){
		if($this->input->get_post("action",true) == 'ajax'){
			$sql_count = "SELECT count(*) as tt FROM {$this->table_}common_admin_nav" ;
			$total =  $this->M_common->query_count($sql_count) ;	
			$result = $this->M_common->querylist("SELECT * FROM {$this->table_}common_admin_nav ORDER BY disorder,id DESC ");		
 			if($result){
				foreach($result as $k=>$v){
					$result[$k]['status'] = ( $v['status'] == 1 )?'开启':'关闭' ;
					if($v['pid'] != 0 ){
						$result[$k]['_parentId'] = $v['pid'];						
					}					
				}
			}
			$data_ = array(
				'total'=>$total,
				'rows'=>$result
				
			);	
			echo result_to_json($data_);
			exit();
		}else{
			$this->load->view("views_admin_nav");
		}			
	}
	
	public function addnav(){
		$action = $this->input->get_post("action",true) ;
		$action_array = array('get_nav','dopost');
		
		if($action == 'get_nav'){			
		
		}elseif($action == 'dopost'){
			//处理添加的数据
			$name = strip_tags(html_escape(daddslashes($this->input->get_post("nav_name",true)))) ;  //导航名称
			$myaction = strip_tags(html_escape(daddslashes($this->input->get_post("myaction",true)))) ;  //导航名称
			$status = !in_array($this->input->get_post("status",true) ,array(0,1))?0:$this->input->get_post("status",true);//状态
			$url = strip_tags(html_escape(daddslashes($this->input->get_post("url",true)))) ;  //url地址
			$disorder = verify_id($this->input->get_post("disorder",true) ); //排序
			$pid = intval($this->input->get_post("pid",true)); //父级id
			$add_time = date("Y-m-d H:i:s",time()); //添加日期
			
			$data = array(
				'name'=>$name,
				'status'=>$status,
				'pid'=>$pid,
				'addtime'=>$add_time,
				'disorder'=>$disorder,
				'url'=>$url,
				'action'=>$myaction
				
			);
			$result_data = array() ;
			$array = $this->M_common->insert_one($data,"{$this->table_}common_admin_nav");
			if($array['affect_num']>=1){		
				echo result_to_json($result_data,1,'success');
				write_action_log($array['sql'],'log_nav_manager_add',"{$this->username}",$this->ip,1,"用户{$this->username}添加导航{$name}成功");
				exit();
			}else{
				write_action_log($array['sql'],'log_nav_manager_add',"{$this->username}",$this->ip,0,"用户{$this->username}添加导航{$name}失败");
				echo result_to_json($result_data,0,'服务器繁忙请稍候');
				exit() ;			
			}
	
		}

	}
	
	//删除导航
	public function deletenav(){
		$id = intval($this->input->get_post("id",true)); //id
		$sql = "DELETE FROM {$this->table_}common_admin_nav WHERE id = '{$id}' ";
		if($id <= 0 ){
			echo result_to_json('',0,'请选择删除');
			write_action_log("{$sql}",'log_nav_manager_del',"{$this->username}",$this->ip,0,"用户{$this->username}删除导航失败");
			exit();
		}
		$num = $this->M_common->del_data($sql);
		if($num<1){
			echo result_to_json('',0,'删除失败,服务器繁忙请稍后');
			write_action_log("{$sql}",'log_nav_manager_del',"{$this->username}",$this->ip,0,"用户{$this->username}删除导航ID是{$id}失败");
			die();
		}else{
			echo result_to_json('',1,'删除成功');
			write_action_log("{$sql}",'log_nav_manager_del',"{$this->username}",$this->ip,1,"用户{$this->username}删除导航ID是{$id}成功");
			die();
		}
	}
	public function editnav(){
		//编辑导航
		
		$action = $this->input->get_post("action",true); //action
	
		$id = intval($this->input->get_post("id",true)); //id
		$action_array = array(
			'show','do'
		);
		$action = in_array($action, $action_array)?$action:'show';
		if($action == 'show'){
			//根据ID获取数据
		
			$sql_one = "SELECT * FROM {$this->table_}common_admin_nav WHERE id = '{$id}' LIMIT 1 ";
			$one_data = $this->M_common->query_one($sql_one);
			
			echo result_to_json($one_data,1,'success') ;
			die() ;
		
		}elseif($action == 'do'){
			
			$array_status = array(0,1);
			$status = !in_array($this->input->get_post("status",true), $array_status)?0:$this->input->get_post("status",true) ;
			$url = strip_tags(html_escape(daddslashes($this->input->get_post("url",true)))) ;  //url地址
			$name = strip_tags(html_escape(daddslashes($this->input->get_post("name",true)))) ;  //导航名称
			$action = strip_tags(html_escape(daddslashes($this->input->get_post("myaction",true)))) ; //操作动作
			$disorder = intval($this->input->get_post("disorder",true) ); //排序
			$sql_edit = "UPDATE `{$this->table_}common_admin_nav` SET `status` = '{$status}' , `url` = '{$url}',`disorder` = '{$disorder}' , `name` = '{$name}',`action` = '{$action}' where id = '{$id}'" ;
	
			$num = $this->M_common->update_data($sql_edit);
			if($num>=1){
				
				echo result_to_json('',1,'success');
				write_action_log("{$sql_edit}",'log_nav_manager_edit',"{$this->username}",$this->ip,1,"用户{$this->username}修改导航{$name}成功");
				die();			
			}else{
				echo result_to_json('',0,"修改导航{$name}失败,请传递正确的参数");
				write_action_log("{$sql_edit}",'log_nav_manager_edit',"{$this->username}",$this->ip,0,"用户{$this->username}修改导航{$name}失败");
				die();			
			}		
		}
		

	}
	//生成导航缓存
	public function make_nav_cache(){
		$total =  $this->M_common->query_count("SELECT count(*) as tt FROM {$this->table_}common_admin_nav where status = 1 ") ;	
		if($total<=0){
			echo result_to_json('',0,'服务器没数据') ;
			exit();
		}	
		
		$result = $this->M_common->querylist("SELECT * FROM {$this->table_}common_admin_nav where `status` = 1  ORDER BY disorder,id DESC ");	
		if(!is_really_writable($this->nav_cache_path)){
			echo result_to_json('',0,'目录'.$this->nav_cache_path."不可写") ;
			die();
		}
		if(!file_exists($this->nav_cache_path."/nav_cache")){
			s_mkdir($this->nav_cache_path."/cache_nav");
		}
		$nav_file = $this->nav_cache_path."/cache_nav/nav.inc.php";
		
		$fp = fopen($nav_file,'w');
		if(!$fp){
			echo result_to_json('',0,"服务器繁忙请稍候") ;
			die();
		}
		flock($fp,3);
		
    	fwrite($fp,"<"."?php\r\n");
    	fwrite($fp,"/*导航缓存文件*/\r\n");
    	fwrite($fp,"/*author wangjian*/\r\n");
    	fwrite($fp,"/*time 2014_01_29*/\r\n");
    	fwrite($fp,"\$nav_array = array(\r\n");
		foreach ($result as $r_key=>$r_val){
			$id = $r_val['id'];
			fwrite($fp,"\$nav_array[$id] =array('id'=>'{$r_val['id']}','name'=>'{$r_val['name']}','pid'=>'{$r_val['pid']}','disorder'=>'{$r_val['disorder']}','url'=>'{$r_val['url']}' );\r\n");
		}
		fwrite($fp,");\r\n");
		fwrite($fp,"?".">");
    	fclose($fp);
    	echo result_to_json('',1,"已经成功的更新导航缓存,请刷新页面看效果") ;
    	write_action_log("no sql",'log_nav_manager_make',"{$this->username}",$this->ip,1,"用户{$this->username}生成导航缓存成功");
	}
	
}
